﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YArchitech.MEP.Electric.CableLay
{
	public class CableTraySchemaDim
	{
		public static void SetDimData(Element element, Guid guid, string schemaName, ElementId entId)
		{
			if (element == null || guid == Guid.Empty || entId == null)
			{
				return;
			}
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName(schemaName);
				schemaBuilder.AddSimpleField("DimID", typeof(ElementId));
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField("DimID");
			entity.Set<ElementId>(field, entId);
			element.SetEntity(entity);
		}

		public static ElementId GetDimData(Element element, Guid guid)
		{
			if (element == null || guid == Guid.Empty)
			{
				return null;
			}
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				return null;
			}
			Entity entity = element.GetEntity(schema);
			if (entity == null || entity.Schema == null)
			{
				return null;
			}
			Field field = schema.GetField("DimID");
			return entity.Get<ElementId>(field);
		}

		private const string m_dimId = "DimID";
	}
}
